<?php

class Lotto
{

    public $data = NULL;

    public function Lotto($lotto_id = 0)
    {
        if (0 < $lotto_id)
        {
            $this->Get($lotto_id);
        }
    }

    public function Get($lotto_id)
    {
        $rs = getdb("select * from lotto where id=" . $lotto_id);
        if ($rs->eof)
        {
            $this->data = null;
        }
        else
        {
            $this->data = $rs->f;
        }
    }

    public function GetHistory($minday, $today)
    {
        $rs = getdb("select id from lotto where open BETWEEN '{$minday}' AND '{$today}' order by open desc");
        $ret = array();
        while (!$rs->eof)
        {
            $ret[] = $rs->f['id'];
            $rs->movenext();
        }
        return $ret;
    }

}

function check_autorate($lotto_id, $bettype, $numbers, $money)
{
    global $g_autorate;
    global $curruser;
    if ($money == 0)
    {
        return;
    }
    if ($bettype == 24)
    {
        $bettype = 0;
    }
    if ($bettype == 26)
    {
        $bettype = 25;
    }
    if ($bettype == 31)
    {
        $bettype = 4;
    }
    if ($bettype == 37)
    {
        $bettype = 21;
    }
    if ($bettype == 38)
    {
        $bettype = 22;
    }
    if (!in_array($bettype, $g_autorate))
    {
        return;
    }
    $conf = new Config();
    $arRs = getdb("SELECT * FROM autorate_config_6h WHERE bettype_id={$bettype} AND subid=0 LIMIT 1");
    if ($arRs->eof)
    {
        return;
    }
    $mm1 = $arRs->f['money1'];
    $mm = $arRs->f['money'];
    if ($mm1 == 0)
    {
        return;
    }
    if ($mm == 0)
    {
        $mm = $mm1;
    }
    $down = $arRs->f['downrate'];
    $down1 = $arRs->f['downrate1'];
    $up = $arRs->f['uprate'];
    $up1 = $arRs->f['uprate1'];
    $limit = $arRs->f['limitrate'];
    if ($bettype == 19)
    {
        $numbers = substr($numbers, 0, 4);
    }
    $sqlCmd = "INSERT INTO autorate (lotto_id,bettype,numbers,amount,total) VALUES ({$lotto_id},{$bettype},'{$numbers}',{$money},{$money}) ON DUPLICATE KEY UPDATE amount = amount +{$money} ,total=total+{$money}";
    getdb($sqlCmd);
    $rs = getdb("!!SELECT amount,total FROM autorate where lotto_id=" . $lotto_id . " and bettype=" . $bettype . " and numbers='" . $numbers . "'");
    $downrate = 0;
    $uprate = 0;
    if ($rs->f['amount'] < $mm1 && $rs->f['total'] == $rs->f['amount'])
    {
        return;
    }
    if ($mm1 <= $rs->f['amount'] && $rs->f['total'] == $rs->f['amount'])
    {
        $xRate = intval(( $rs->f['amount'] - $mm1 ) / $mm);
        $downrate = $down1 + $down * $xRate;
        $uprate = $up1 + $up * $xRate;
        $money = $rs->f['amount'] - ( $rs->f['amount'] - $mm1 ) % $mm;
    }
    else if ($mm <= $rs->f['amount'] && $mm1 < $rs->f['total'])
    {
        $xRate = intval($rs->f['amount'] / $mm);
        $downrate = $down * $xRate;
        $uprate = $up * $xRate;
        $money = $rs->f['amount'] - $rs->f['amount'] % $mm;
    }
    else
    {
        return;
    }
    $rs->close();
    if ($downrate <= 0)
    {
        return;
    }
    switch ($bettype)
    {
        case 0 :
        case 24 :
            $rsa = getdb("select r" . $numbers . " from rate0a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate)
            {
                getdb("update rate0a set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate0b set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate0c set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate0d set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            }
            else if ($rsa->f[0] - $downrate < $limit && $limit < $rsa->f[0])
            {
                getdb("update rate0a set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate0b set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate0c set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate0d set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
            }
            $rsa = getdb("select r" . $numbers . " from rate24a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate)
            {
                getdb("update rate24a set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate24b set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate24c set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate24d set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            }
            else if ($rsa->f[0] - $downrate < $limit && $limit < $rsa->f[0])
            {
                getdb("update rate24a set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate24b set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate24c set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate24d set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            break;
        case 4 :
        case 31 :
            $rsa = getdb("select r" . $numbers . " from rate4a where lotto_id=" . $lotto_id);
            getdb("update rate4a set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            getdb("update rate4b set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            getdb("update rate4c set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            getdb("update rate4d set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            if ($rsa->f[0] - $downrate < $limit && $limit < $rsa->f[0])
            {
                getdb("update rate4a set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate4b set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate4c set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate4d set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
            }
            $rsa = getdb("select r" . $numbers . " from rate31a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate)
            {
                getdb("update rate31a set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate31b set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate31c set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate31d set r" . $numbers . "=r" . $numbers . "-" . $downrate . " where lotto_id=" . $lotto_id);
            }
            else if ($rsa->f[0] - $downrate < $limit && $limit < $rsa->f[0])
            {
                getdb("update rate31a set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate31b set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate31c set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate31d set r" . $numbers . "=" . $limit . " where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            break;
        case 25 :
        case 26 :
            $nn = split(" ", $numbers);
            $rsa = getdb("select r" . $nn[1] . " from rate4" . $nn[0] . "a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate)
            {
                getdb("update rate4" . $nn[0] . "a set r" . $nn[1] . "=r" . $nn[1] . "-" . $downrate . ", br" . $nn[1] . "=br" . $nn[1] . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate4" . $nn[0] . "b set r" . $nn[1] . "=r" . $nn[1] . "-" . $downrate . ", br" . $nn[1] . "=br" . $nn[1] . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate4" . $nn[0] . "c set r" . $nn[1] . "=r" . $nn[1] . "-" . $downrate . ", br" . $nn[1] . "=br" . $nn[1] . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate4" . $nn[0] . "d set r" . $nn[1] . "=r" . $nn[1] . "-" . $downrate . ", br" . $nn[1] . "=br" . $nn[1] . "-" . $downrate . " where lotto_id=" . $lotto_id);
            }
            else if ($rsa->f[0] - $downrate < $limit && $limit < $rsa->f[0])
            {
                getdb("update rate4" . $nn[0] . "a set r" . $nn[1] . "=" . $limit . ", br" . $nn[1] . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate4" . $nn[0] . "b set r" . $nn[1] . "=" . $limit . ", br" . $nn[1] . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate4" . $nn[0] . "c set r" . $nn[1] . "=" . $limit . ", br" . $nn[1] . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate4" . $nn[0] . "d set r" . $nn[1] . "=" . $limit . ", br" . $nn[1] . "=" . $limit . " where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            break;
        case 1 :
        case 2 :
        case 3 :
        case 20 :
        case 28 :
        case 30 :
        case 35 :
        case 47 :
            $n1 = "r" . $bettype . "_1";
            $n2 = "r" . $bettype . "_2";
            if ($numbers == 2)
            {
                $n1 = "r" . $bettype . "_2";
                $n2 = "r" . $bettype . "_1";
            }
            $rsa = getdb("select " . $n1 . "," . $n2 . " from rate1a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate && $limit < $rsa->f[1] - $downrate)
            {
                getdb("update rate1a set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
                getdb("update rate1b set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
                getdb("update rate1c set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
                getdb("update rate1d set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
            }
            else if ($limit < min($rsa->f[0], $rsa->f[1]))
            {
                $n_s = max($rsa->f[0], $rsa->f[1]) - min($rsa->f[0], $rsa->f[1]) + $up * 2;
                $n_d = $limit;
                $n_u = $limit + $n_s;
                getdb("update rate1a set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
                getdb("update rate1b set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
                getdb("update rate1c set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
                getdb("update rate1d set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            break;
        case 13 :
        case 14 :
        case 18 :
        case 21 :
        case 22 :
        case 34 :
        case 46 :
            $n1 = "r" . $bettype . "_" . $numbers;
            $rsa = getdb("select " . $n1 . " from rate1a where lotto_id=" . $lotto_id);
            $orate = $rsa->f[0];
            $n2Sql = "";
            if ($limit < $rsa->f[0] - $downrate)
            {
                $nrate = $rsa->f[0] - $downrate;
                if ($bettype == 21)
                {
                    $n2Sql = ", r37_{$numbers} = r37_{$numbers} - {$downrate}";
                }
                else if ($bettype == 22)
                {
                    $n2Sql = ", r38_{$numbers} = r38_{$numbers} - {$downrate}";
                }
                getdb("update rate1a set " . $n1 . "=" . $n1 . "-" . $downrate . " {$n2Sql} where lotto_id=" . $lotto_id);
                getdb("update rate1b set " . $n1 . "=" . $n1 . "-" . $downrate . " {$n2Sql} where lotto_id=" . $lotto_id);
                getdb("update rate1c set " . $n1 . "=" . $n1 . "-" . $downrate . " {$n2Sql} where lotto_id=" . $lotto_id);
                getdb("update rate1d set " . $n1 . "=" . $n1 . "-" . $downrate . " {$n2Sql} where lotto_id=" . $lotto_id);
            }
            else if ($limit < $rsa->f[0])
            {
                $nrate = $limit;
                if ($bettype == 21)
                {
                    $n2Sql = ", r37_{$numbers} = {$limit}";
                }
                else if ($bettype == 22)
                {
                    $n2Sql = ", r38_{$numbers} = {$limit}";
                }
                getdb("update rate1a set " . $n1 . "=" . $limit . " {$n2Sql} where lotto_id=" . $lotto_id);
                getdb("update rate1b set " . $n1 . "=" . $limit . " {$n2Sql} where lotto_id=" . $lotto_id);
                getdb("update rate1c set " . $n1 . "=" . $limit . " {$n2Sql} where lotto_id=" . $lotto_id);
                getdb("update rate1d set " . $n1 . "=" . $limit . " {$n2Sql} where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            $rr = 0 < $orate ? round($nrate / $orate, 4) : 0;
            if (0 < $rr && $bettype == 21 && intval($conf->get("bettype21_27")) == 1)
            {
                $field = "r" . d2($numbers);
                $subid = 2;
                while ($subid <= 5)
                {
                    getdb("update rate27 set " . $field . " = ROUND(" . $field . " * " . $rr . ",2) where lotto_id=" . $lotto_id . " and disk IN (1,2,3,4) and subid={$subid}");
                    ++$subid;
                }
            }
            break;
        case 19 :
            $n0 = substr($numbers, 0 - 3);
            switch (intval(substr($numbers, 0, 1)))
            {
                case 1 :
                    $n1 = "r19_1" . $n0;
                    break;
                case 2 :
                    $n1 = "r19_2" . $n0;
                    break;
                default :
                    return;
                    break;
            }
            $rsa = getdb("select " . $n1 . " from rate1a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate)
            {
                getdb("update rate1a set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate1b set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate1c set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id=" . $lotto_id);
                getdb("update rate1d set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id=" . $lotto_id);
            }
            else if ($limit < $rsa->f[0])
            {
                getdb("update rate1a set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate1b set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate1c set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
                getdb("update rate1d set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            break;
        case 5 :
        case 6 :
            $n1 = "r" . $bettype . "_1";
            $n2 = "r" . $bettype . "_2";
            if ($numbers == 2)
            {
                $n1 = "r" . $bettype . "_2";
                $n2 = "r" . $bettype . "_1";
            }
            $rsa = getdb("select " . $n1 . "," . $n2 . " from rate5a where lotto_id=" . $lotto_id);
            if ($limit < $rsa->f[0] - $downrate && $limit < $rsa->f[1] - $downrate)
            {
                getdb("update rate5a set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
                getdb("update rate5b set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
                getdb("update rate5c set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
                getdb("update rate5d set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id=" . $lotto_id);
            }
            else if ($limit < min($rsa->f[0], $rsa->f[1]))
            {
                $n_s = max($rsa->f[0], $rsa->f[1]) - min($rsa->f[0], $rsa->f[1]) + $up * 2;
                $n_d = $limit;
                $n_u = $limit + $n_s;
                getdb("update rate5a set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
                getdb("update rate5b set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
                getdb("update rate5c set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
                getdb("update rate5d set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            }
            $rsa->close();
            break;
        case 15 :
        case 16 :
        case 17 :
        case 23 :
            $nn = split(" ", $numbers);
            autorate15($nn[0], $nn[1], $lotto_id, $downrate, $uprate, $limit, $up);
            break;
        case 7 :
        case 8 :
        case 9 :
        case 33 :
        case 10 :
        case 32 :
        case 11 :
        case 12 :
        case 29 :
        case 39 :
        case 40 :
        case 41 :
        case 42 :
        case 43 :
        case 44 :
        case 45 :
            if ($numbers == "" || intval($numbers) == 0)
            {
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "执行五不中时，number 无传值，请检查系统");
                diemsg("执行五不中时，number 无传值，请检查系统");
            }
            $numbers = str_pad($numbers, 2, "0", STR_PAD_LEFT);
            $n1 = "r{$numbers}";
            $rsa = getdb("select {$n1} from rate29 where lotto_id={$lotto_id} and disk = 1 limit 1");
            if ($limit < $rsa->f[0] - $downrate)
            {
                getdb("update rate29 set {$n1}={$n1}-{$downrate} where lotto_id={$lotto_id} and bettype={$bettype} and disk IN (1,2,3,4)");
            }
            else if ($limit < $rsa->f[0])
            {
                getdb("update rate29 set {$n1}={$limit} where lotto_id={$lotto_id} and bettype={$bettype} and disk IN (1,2,3,4)");
            }
            $rsa->close();
            break;
    }
    getdb("update autorate set amount=amount-" . $money . " where lotto_id=" . $lotto_id . " and bettype=" . $bettype . " and numbers='" . $numbers . "'");
}

function autorate15($num1, $num2, $lotto_id, $downrate, $uprate, $limit, $up)
{
    if ($num2 <= 4)
    {
        switch ($num2)
        {
            case 1 :
                $n1 = "r15_" . $num1 . "_1";
                $n2 = "r15_" . $num1 . "_2";
                break;
            case 2 :
                $n1 = "r15_" . $num1 . "_2";
                $n2 = "r15_" . $num1 . "_1";
                break;
            case 3 :
                $n1 = "r15_" . $num1 . "_3";
                $n2 = "r15_" . $num1 . "_4";
                break;
            case 4 :
                $n1 = "r15_" . $num1 . "_4";
                $n2 = "r15_" . $num1 . "_3";
                break;
        }
        $rsa = getdb("select " . $n1 . "," . $n2 . " from rate15a where lotto_id=" . $lotto_id);
        if ($limit < $rsa->f[0] - $downrate && $limit < $rsa->f[1] - $downrate)
        {
            getdb("update rate15a set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
            getdb("update rate15b set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
            getdb("update rate15c set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
            getdb("update rate15d set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
        }
        else if ($limit < min($rsa->f[0], $rsa->f[1]))
        {
            $n_s = max($rsa->f[0], $rsa->f[1]) - min($rsa->f[0], $rsa->f[1]) + $up * 2;
            $n_d = $limit;
            $n_u = $limit + $n_s;
            getdb("update rate15a set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            getdb("update rate15b set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            getdb("update rate15c set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            getdb("update rate15d set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
        }
        $rsa->close();
    }
    else if (8 <= $num2)
    {
        if ($num2 == 8)
        {
            $n1 = "r15_" . $num1 . "_8";
            $n2 = "r15_" . $num1 . "_9";
        }
        else
        {
            $n1 = "r15_" . $num1 . "_9";
            $n2 = "r15_" . $num1 . "_8";
        }
        $rsa = getdb("select " . $n1 . "," . $n2 . " from rate15a where lotto_id=" . $lotto_id);
        if ($limit < $rsa->f[0] - $downrate && $limit < $rsa->f[1] - $downrate)
        {
            getdb("update rate15a set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
            getdb("update rate15b set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
            getdb("update rate15c set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
            getdb("update rate15d set " . $n1 . "=" . $n1 . "-" . $downrate . "," . $n2 . "=" . $n2 . "+" . $uprate . " where lotto_id = " . $lotto_id);
        }
        else if ($limit < min($rsa->f[0], $rsa->f[1]))
        {
            $n_s = max($rsa->f[0], $rsa->f[1]) - min($rsa->f[0], $rsa->f[1]) + $up * 2;
            $n_d = $limit;
            $n_u = $limit + $n_s;
            getdb("update rate15a set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            getdb("update rate15b set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            getdb("update rate15c set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
            getdb("update rate15d set " . $n1 . "=" . $n_d . "," . $n2 . "=" . $n_u . " where lotto_id=" . $lotto_id);
        }
        $rsa->close();
    }
    else
    {
        $n1 = "r15_" . $num1 . "_" . $num2;
        $rsa = getdb("select " . $n1 . " from rate15a where lotto_id=" . $lotto_id);
        if ($limit < $rsa->f[0] - $downrate)
        {
            getdb("update rate15a set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id = " . $lotto_id);
            getdb("update rate15b set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id = " . $lotto_id);
            getdb("update rate15c set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id = " . $lotto_id);
            getdb("update rate15d set " . $n1 . "=" . $n1 . "-" . $downrate . " where lotto_id = " . $lotto_id);
        }
        else if ($limit < $rsa->f[0])
        {
            getdb("update rate15a set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
            getdb("update rate15b set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
            getdb("update rate15c set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
            getdb("update rate15d set " . $n1 . "=" . $limit . " where lotto_id=" . $lotto_id);
        }
        $rsa->close();
    }
}

function GetMyAmount($uid, $tbl = "")
{
    $op = 0;
    $rs = getdb("select id,prev_id from k_user where id=" . $uid);
    if ($rs->eof)
    {
        $rs->close();
        return 0;
    }
    $rs1 = getdb("select amount{$tbl} from k_user where id=" . $rs->f['prev_id']);
    if ($rs1->f["amount{$tbl}"] == 0)
    {
        $rs1->close();
        return 0;
    }
    $rs2 = getdb("select sum(amount{$tbl}) as amount{$tbl} from k_user where prev_id=" . $rs->f['prev_id'] . " and id<>" . $rs->f['id']);
    $op = $rs1->f["amount{$tbl}"] - $rs2->f["amount{$tbl}"];
    $op = $op < 0 ? 0 : $op;
    $rs->close();
    $rs1->close();
    $rs2->close();
    return $op;
}

function GetMyFreeAmount($uid, $tbl = "")
{
    $op = 0;
    $rs = getdb("select amount{$tbl} from k_user where id=" . $uid);
    if ($rs->eof)
    {
        $rs->close();
        return 0;
    }
    $rs1 = getdb("select sum(amount{$tbl}) as amount{$tbl} from k_user where prev_id=" . $uid);
    $op = $rs->f["amount{$tbl}"] - $rs1->f["amount{$tbl}"];
    $rs->close();
    $rs1->close();
    $op = $op < 0 ? 0 : $op;
    return $op;
}

function Title($s, $s2 = "", $wh = 758)
{
    global $g_color;
    global $gametype;
    if (!isset($gametype))
    {
        $cls = "title0";
    }
    else if (0 < $gametype)
    {
        $cls = "bgImg_{$gametype}";
    }
    else
    {
        $cls = "title0";
    }
    $tmp = "<table bgcolor=" . $g_color . " border=0 cellpadding=0 cellspacing=1 width=" . $wh . "><tr><td>";
    $tmp .= "<table class=table0 width='100%' border=0 cellpadding=0 cellspacing=0>";
    $tmp .= "<tr><td class='{$cls}'>";
    $tmp .= "<table width=100% border=0 cellpadding=0 cellspacing=0>";
    $tmp .= "<tr><td>" . $s . "</td>";
    if ($s2 != "")
    {
        $tmp .= "<td align=right>" . $s2 . "</td>";
    }
    $tmp .= "</table>";
    $tmp .= "</tr></table>";
    $tmp .= "</td></tr></table>";
    return $tmp;
}

function GetUserList($uid, $acl = 1, $enabled = 1, $lastlogdtInt = 0)
{
    $r = "";
    $rs = getdb("select acl from k_user where id=" . $uid);
    if ($rs->eof)
    {
        return false;
    }
    if ($rs->f['acl'] == $acl)
    {
        return $uid;
    }
    if ($rs->f['acl'] == 3 && $acl == 0 - 1)
    {
        $rs1 = getdb("select id from k_user where acl=" . $acl . " and prev_id=" . $uid);
        while (!$rs1->eof)
        {
            $r .= $rs1->f['id'] . ",";
            $rs1->movenext();
        }
        $rs1->close();
        if (substr($r, strlen($r) - 1, 1) == ",")
        {
            $r = substr($r, 0, strlen($r) - 1);
        }
        return $r;
    }
    $curracl = $rs->f['acl'] - 1;
    if ($acl <= $curracl)
    {
        $rs1 = getdb("select id, lastlogdt from k_user where acl=" . $curracl . " and prev_id=" . $uid);
        while (!$rs1->eof)
        {
            if ($curracl == $acl)
            {
                if ($curracl == 1 && 0 < $lastlogdtInt)
                {
                    if ($lastlogdtInt < $rs1->f['lastlogdt'])
                    {
                        $r .= $rs1->f['id'] . ",";
                    }
                }
                else
                {
                    $r .= $rs1->f['id'] . ",";
                }
            }
            else
            {
                $users = GetUserList($rs1->f['id'], $acl, $enabled);
                if ($users != "")
                {
                    $r .= $users . ",";
                }
            }
            $rs1->movenext();
        }
        $rs1->close();
    }
    $rs->close();
    if (substr($r, strlen($r) - 1, 1) == ",")
    {
        $r = substr($r, 0, strlen($r) - 1);
    }
    return $r;
}

function GetAllUserList($uid)
{
    $r = "";
    $rs = getdb("select id from k_user where prev_id=" . $uid . " and enabled=1");
    if ($rs->eof)
    {
        return "";
    }
    while (!$rs->eof)
    {
        $r1 = GetAllUserList($rs->f['id']);
        if ($r1 != "")
        {
            $r .= $r1 . ",";
        }
        $r .= $rs->f['id'] . ",";
        $rs->movenext();
    }
    $rs->close();
    $r = substr($r, 0, strlen($r) - 1);
    return $r;
}

function pos2($name, $link = "", $target = "")
{
    if (G_TEMPLATE == "k2")
    {
        return pos4($name, $link, $target);
    }
    $color1 = "#CC6600";
    $color2 = "#7A7A7A";
    $r = "<td nowrap onmouseover=\"this.style.backgroundColor='{$color1}'\" onmouseout=\"this.style.backgroundColor='{$color2}'\" align=\"center\">";
    if ($link != "")
    {
        $r .= "<a href=\"" . $link . "\"";
    }
    if ($target != "")
    {
        $r .= " target=\"" . $target . "\"";
    }
    if ($link != "")
    {
        $r .= " class='link'>";
    }
    $r .= $name;
    if ($link != "")
    {
        $r .= "</a>";
    }
    $r .= "</td>";
    return $r;
}

function pos3($name, $link, $target = "")
{
    $r = "<td background=image/menu_2_03a.gif height=33 width=220><nobr> &nbsp; &nbsp; &nbsp; &nbsp;<a onmouseover=pos3_over() onmouseout=pos3_out() class=whitelink href=\"" . $link . "\" ";
    $r .= " target=\"" . $target . "\"";
    $r .= ">" . $name . "</a></nobr></td>";
    return $r;
}

function pos4($name, $link = "", $target = "")
{
    $r = "";
    if ($link != "")
    {
        $r .= "<a href=\"" . $link . "\"";
    }
    if ($target != "")
    {
        $r .= " target=\"" . $target . "\"";
    }
    if ($link != "")
    {
        $r .= " class='title1'>";
    }
    $r .= $name;
    if ($link != "")
    {
        $r .= "</a>&nbsp;";
    }
    return $r;
}

function trimfloat($f, $use_input = 0)
{
    return $f < 0 ? 0 : floatval(round($f, 3));
}

function GetBetContent($bettype, $numbers, $amount = 0)
{
    global $g_betname;
    global $g_bignum;
    global $g_chinayear;
    global $g_colorname;
    $name1 = array("", "单", "双", "大", "小", "红波", "蓝波", "绿波", "合单", "合双");
    $name2 = array("", "一", "二", "三", "四", "五", "六");
    switch ($bettype)
    {
        case 0 :
            return "特码 " . d2($numbers);
        case 1 :
            return "特码 " . ( $numbers == 1 ? "单" : "双" );
        case 2 :
            return "特码 " . ( $numbers == 1 ? "大" : "小" );
        case 46 :
            $arr = array(1 => "大", 2 => "中", 3 => "小");
            return "特大中小-" . $arr[$numbers];
        case 3 :
            return "特码 " . ( $numbers == 1 ? "合单" : "合双" );
        case 47 :
            return "特码 " . ( $numbers == 1 ? "合大" : "合小" );
        case 4 :
            return "正码 " . d2($numbers);
        case 5 :
            return "总 " . ( $numbers == 1 ? "单" : "双" );
        case 6 :
            return "总 " . ( $numbers == 1 ? "大" : "小" );
        case 7 :
        case 9 :
        case 33 :
        case 10 :
        case 32 :
        case 11 :
            $p = explode("|", $numbers);
            $s = $g_betname[$bettype] . "<font color=#217952>@" . $p[1] . "</font>";
            if (10 < $p[2])
            {
                if (100 < $p[2])
                {
                    $s .= "<br>" . $p[3] . "<br><b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
                }
                else
                {
                    $s .= "<br>" . $p[3] . "&nbsp;&nbsp;<b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
                }
            }
            else
            {
                $s .= "&nbsp;&nbsp;" . $p[3] . "&nbsp;&nbsp;<b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
            }
            return $s;
		case 48 :
        case 8 :
            $p = explode("|", $numbers);
            $s = $g_betname[$bettype] . "<font color=#217952>@" . $p[1] . "</font>";
            if (8 < $p[2])
            {
                if (100 < $p[2])
                {
                    $s .= "<br>" . $p[3] . "<br><b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
                }
                else
                {
                    $s .= "<br>" . $p[3] . "&nbsp;&nbsp;<b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
                }
            }
            else
            {
                $s .= "&nbsp;&nbsp;" . $p[3] . "&nbsp;&nbsp;<b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
            }
            return $s;
        case 12 :
            $p = $g_betname[$bettype] . "<br>";
            $p1 = explode(",", $numbers);
            foreach ($p1 as $p2)
            {
                $p3 = explode("_", $p2);
                $p .= "正码" . $name2[$p3[0]] . "" . $name1[$p3[1]] . " @ " . $p3[2] . "<br>";
            }
            return $p;
        case 13 :
            return "特码 " . $g_colorname[$numbers] . "波";
        case 14 :
            return "特码生肖 " . $g_chinayear[$numbers];
        case 15 :
        case 16 :
        case 17 :
        case 23 :
            $p = explode(" ", $numbers);
            return "正码" . $name2[$p[0]] . " " . $name1[$p[1]];
        case 18 :
            $p0 = "";
            $p1 = "";
            switch ($numbers)
            {
                case 4 :
                case 3 :
                case 2 :
                case 1 :
                    $p0 = 1;
                    $p1 = $numbers;
                    break;
                case 8 :
                case 7 :
                case 6 :
                case 5 :
                    $p0 = 2;
                    $p1 = $numbers - 4;
                    break;
                case 12 :
                case 11 :
                case 10 :
                case 9 :
                    $p0 = 3;
                    $p1 = $numbers - 8;
                    break;
            }
            if ($p1 < 3)
            {
                return "特码半波 " . $g_colorname[$p0] . ( $p1 == 1 ? "单" : "双" );
            }
            return "特码半波 " . $g_colorname[$p0] . ( $p1 == 3 ? "大" : "小" );
        case 19 :
            $p1 = explode("|", $numbers);
            $p3 = explode("_", $p1[0]);
            $n1 = $p3[0];
            $n2 = intval($p3[1]);
            $op = "特码 " . $n2 . " 肖 " . ( $n1 == 1 ? "  中" : "不中" ) . "<br>";
            if (1 < count($p1))
            {
                $p2 = explode(",", $p1[1]);
                $op .= $g_chinayear[$p2[0]];
                $i = 1;
                while ($i < $n2)
                {
                    $op .= "," . $g_chinayear[$p2[$i]];
                    ++$i;
                }
            }
            return $op;
        case 20 :
            return "特码 " . ( $numbers == 1 ? "家禽" : "野兽" );
        case 21 :
            return "一肖 " . $g_chinayear[$numbers];
        case 37 :
            return "一肖 " . $g_chinayear[$numbers];
        case 22 :
            return "尾数 " . substr($numbers, 0 - 1);
        case 38 :
            return "尾数 " . substr($numbers, 0 - 1);
        case 23 :
            $p = explode(" ", $numbers);
            return "正码" . $name2[$p[0]] . ( $p[1] == 8 ? " 合单" : " 合双" );
        case 24 :
            return "特码 " . d2($numbers);
        case 25 :
            $p = explode(" ", $numbers);
            return "正码特" . $name2[$p[0]] . " " . d2($p[1]);
        case 26 :
            $p = explode(" ", $numbers);
            return "正码特" . $name2[$p[0]] . " " . d2($p[1]);
        case 27 :
            $p1 = explode("|", $numbers);
            $p2 = explode(",", $p1[1]);
            $op = $g_bignum[$p1[0]] . "肖 ";
            $i = 0;
            while ($i < $p1[0])
            {
                if (0 < $i)
                {
                    $op .= ",";
                }
                $op .= $g_chinayear[$p2[$i]];
                ++$i;
            }
            return $op;
        case 28 :
            return "特尾 " . ( $numbers == 1 ? "大" : "小" );
        case 29 :
        case 39 :
        case 40 :
        case 41 :
        case 42 :
        case 43 :
        case 44 :
        case 45 :
            $p = explode("|", $numbers);
            $s = $g_betname[$bettype] . "<font color=#217952>@" . $p[1] . "</font>";
            if (6 < $p[2])
            {
                $s .= "<br>" . $p[3] . "&nbsp;&nbsp;<b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
            }
            else
            {
                $s .= "&nbsp;" . $p[3] . "&nbsp;&nbsp;<b>" . $p[2] . "</b>组× <b>" . $p[0] . "</b>";
            }
            return $s;
        case 30 :
            return "合尾 " . ( $numbers == 1 ? "大" : "小" );
        case 31 :
            return "正码 " . d2($numbers);
        case 34 :
            return "总肖 " . ( $numbers == 4 ? "234" : $numbers );
        case 35 :
            return "总肖 " . ( $numbers == 1 ? "单" : "双" );
        case 36 :
            $pp = explode("|", $numbers);
            $p1 = intval(substr($pp[0], 0, 1));
            $p2 = intval(substr($pp[0], 1, 1));
            $p3 = explode(",", $pp[1]);
            $op = $g_bignum[$p1] . "尾" . ( $p2 == 1 ? "中" : "不中" );
            $i = 0;
            while ($i < $p1)
            {
                if (0 < $i)
                {
                    $op .= ",";
                }
                $op .= $p3[$i];
                ++$i;
            }
            return $op;
		case 49:
			return "特码头：头". $numbers;
		case 50:
			return "特码尾：尾". $numbers;
    }
}

function show_linechinayear($ccc)
{
    global $g_chinayear;
    $ppp = explode(",", $ccc);
    $cx = count($ppp);
    $cnum = array();
    $i = 0;
    while ($i < $cx)
    {
        $cnum[$i] = $g_chinayear[$ppp[$i]];
        ++$i;
    }
    return implode(",", $cnum);
}

function check_color($num)
{
    global $g_numcolor;
    return $g_numcolor[$num];
}

function GetBack($uid, $bettype, $disk, $acl, $gametype = 0)
{
    static $b = NULL;
    $mod = 0;
    if ($acl == 255)
    {
        if ($uid == "")
        {
            return 0;
        }
        $rs = getdb("select id from k_user where acl=255 and account='" . $uid . "'");
        $uid = $rs->f['id'];
        $mod = 255;
        $rs->close();
    }
    if (!isset($b))
    {
        $b = array();
    }
    if (!isset($b[$uid]))
    {
        $b[$uid] = GetAmountSetting($uid, $gametype, $mod);
        if ($b[$uid] == false)
        {
            diemsg("没有设定退水：user id = {$uid}");
        }
    }
    if ($acl == 255 && $b[$uid][$bettype]["back" . $disk] == 100)
    {
        return 0;
    }
    return $b[$uid][$bettype]["back" . $disk];
}

function show_rate($bettype, $rate)
{
    switch ($bettype)
    {
        case 9 :
        case 10 :
        case 12 :
        case 32 :
        case 33 :
            return "";
    }
    return trimfloat($rate, 1);
}

function GetChinaYearNumber($china = 0)
{
    $oo = array(
        array(1, 13, 25, 37, 49),
        array(12, 24, 36, 48),
        array(11, 23, 35, 47),
        array(10, 22, 34, 46),
        array(9, 21, 33, 45),
        array(8, 20, 32, 44),
        array(7, 19, 31, 43),
        array(6, 18, 30, 42),
        array(5, 17, 29, 41),
        array(4, 16, 28, 40),
        array(3, 15, 27, 39),
        array(2, 14, 26, 38)
    );
    $conf = new Config();
    $y = $conf->get("chinayear");
    $k = 0;
    $g = array();
    $i = $y;
    while ($i <= 12)
    {
        $g[$i] = $oo[$k++];
        ++$i;
    }
    $i = 1;
    while ($i < $y)
    {
        $g[$i] = $oo[$k++];
        ++$i;
    }
    if ($china != 0)
    {
        return $g[$china];
    }
    return $g;
}

function GetChinaYearNumberPic()
{
    $oo = array(
        array("<img src='image/ball_11.jpg'><img src='image/ball_49.jpg'>"),
        array("<img src='image/ball_12.jpg'>"),
        array("<img src='image/ball_1.jpg'>"),
        array("<img src='image/ball_2.jpg'>"),
        array("<img src='image/ball_3.jpg'>"),
        array("<img src='image/ball_4.jpg'>"),
        array("<img src='image/ball_5.jpg'>"),
        array("<img src='image/ball_6.jpg'>"),
        array("<img src='image/ball_7.jpg'>"),
        array("<img src='image/ball_8.jpg'>"),
        array("<img src='image/ball_9.jpg'>"),
        array("<img src='image/ball_10.jpg'>")
    );
    $conf = new Config();
    $y = $conf->get("chinayear");
    $k = 0;
    $g = array();
    $i = $y;
    while ($i <= 12)
    {
        $g[$i] = $oo[$k++];
        ++$i;
    }
    $i = 1;
    while ($i < $y)
    {
        $g[$i] = $oo[$k++];
        ++$i;
    }
    return $g;
}

function GetChinaYearNumberPic6()
{
    $oo = array(
        array("<img src='image/ball_11.jpg'>"),
        array("<img src='image/ball_12.jpg'>"),
        array("<img src='image/ball_1.jpg'>"),
        array("<img src='image/ball_2.jpg'>"),
        array("<img src='image/ball_3.jpg'>"),
        array("<img src='image/ball_4.jpg'>"),
        array("<img src='image/ball_5.jpg'>"),
        array("<img src='image/ball_6.jpg'>"),
        array("<img src='image/ball_7.jpg'>"),
        array("<img src='image/ball_8.jpg'>"),
        array("<img src='image/ball_9.jpg'>"),
        array("<img src='image/ball_10.jpg'>")
    );
    $conf = new Config();
    $y = $conf->get("chinayear");
    $k = 0;
    $g = array();
    $i = $y;
    while ($i <= 12)
    {
        $g[$i] = $oo[$k++];
        ++$i;
    }
    $i = 1;
    while ($i < $y)
    {
        $g[$i] = $oo[$k++];
        ++$i;
    }
    return $g;
}

function Get_DomNumber($g_chinayearnum)
{
    $op = array();
    $j = 0;
    $k = 2;
    while ($k < 13)
    {
        switch ($k)
        {
            case 2 :
            case 7 :
            case 8 :
            case 10 :
            case 11 :
            case 12 :
                $cc = count($g_chinayearnum[$k]);
                $i = 0;
        }
        while ($i < $cc)
        {
            $op[$j] = $g_chinayearnum[$k][$i];
            ++$j;
            ++$i;
        }
        ++$k;
    }
    return $op;
}

function Get_WildNumber($g_chinayearnum)
{
    $op = array();
    $j = 0;
    $k = 1;
    while ($k < 10)
    {
        switch ($k)
        {
            case 1 :
            case 3 :
            case 4 :
            case 5 :
            case 6 :
            case 9 :
                $cc = count($g_chinayearnum[$k]);
                $i = 0;
        }
        while ($i < $cc)
        {
            $op[$j] = $g_chinayearnum[$k][$i];
            ++$j;
            ++$i;
        }
        ++$k;
    }
    return $op;
}

function GetAmountInfo($bettype, $subid = 0)
{
    global $curruser;
    global $lotto_id;
    static $amounts = NULL;
    if (!isset($amounts))
    {
        $amounts = array();
        $rs1 = getdb("select bettype,subid,amount1,amount2 from amounts where user_id=" . $curruser['id'] . " order by bettype");
        while (!$rs1->eof)
        {
            $btype = $rs1->f['bettype'] . ( 0 < intval($rs1->f['subid']) ? $rs1->f['subid'] : "" );
            $amounts[$btype] = array(
                1 => $rs1->f['amount1'],
                2 => $rs1->f['amount2']
            );
            if ($rs1->f['amount1'] != 0 || $rs1->f['amount2'] != 0)
            {
                switch ($rs1->f['bettype'])
                {
                    case 24 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[0][1],
                            2 => $amounts[0][2]
                        );
                        break;
                    case 26 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[25][1],
                            2 => $amounts[25][2]
                        );
                        break;
                    case 31 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[4][1],
                            2 => $amounts[4][2]
                        );
                        break;
                    case 32 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[10][1],
                            2 => $amounts[10][2]
                        );
                        break;
                    case 33 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[9][1],
                            2 => $amounts[9][2]
                        );
                        break;
                    case 37 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[21][1],
                            2 => $amounts[21][2]
                        );
                        break;
                    case 38 :
                        $amounts[$rs1->f['bettype']] = array(
                            1 => $amounts[22][1],
                            2 => $amounts[22][2]
                        );
                        break;
                }
            }
            $rs1->movenext();
        }
        $rs1->close();
    }
    $r = array();
    $btype = intval($bettype . ( 0 < $subid ? $subid : "" ));
    $r[0] = 0;
    $r[1] = $amounts[$btype][1];
    $r[2] = $amounts[$btype][2];
    return $r;
}

function Get_bet_amounts_Info($bettype, $numbers, $subid = 0)
{
    global $curruser;
    global $lotto_id;
    $r = array();
    switch ($bettype)
    {
        case 0 :
        case 24 :
            $bettypeStr = "bettype in (0,24)";
            break;
        case 25 :
        case 26 :
            $bettypeStr = "bettype in (25,26)";
            break;
        case 4 :
        case 31 :
            $bettypeStr = "bettype in (4,31)";
            break;
        case 9 :
        case 33 :
            $bettypeStr = "bettype in (9,33)";
            break;
        case 10 :
        case 32 :
            $bettypeStr = "bettype in (10,32)";
            break;
        case 21 :
        case 37 :
            $bettypeStr = "bettype in (21,37)";
            break;
        case 22 :
        case 38 :
            $bettypeStr = "bettype in (22,38)";
            break;
        default :
            $bettypeStr = "bettype=" . intval($bettype);
            break;
    }
    $rs1 = getdb("!!select amount from bet_amounts where {$bettypeStr} and subid={$subid} and numbers='{$numbers}' and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " LIMIT 1");
    $n = $rs1 === false ? 999999999 : intval($rs1->f[0]);
    $rs1->close();
    return $n;
}
function Get_bet_amounts($bettype, $numbers)
{
    global $curruser;
    global $lotto_id;
    $rs1 = getdb("select sum(amount) as amount,numbers from bet where bettype=" . $bettype . " and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " and rame=0 group by numbers");
                                while (!$rs1->eof)
                                {
                                   if($bettype==25){
										if ($rs1->f['numbers'] =='1'.' '.$numbers)
										{
											$n += $rs1->f['amount'];//已下注每期总额
										}
								   
								   } elseif ($rs1->f['numbers'] == $numbers)
                                    {
                                        $n += $rs1->f['amount'];//已下注每期总额
                                    }
                                    $rs1->movenext();
                                }
    $rs1->close();
    return $n;
}
function say_numbercolor($n, $dic = 2)
{
    $cc = "";
    if ($n < 0)
    {
        $cc = "<font color=#d80000>" . sprintf("%." . $dic . "f", $n) . "</font>";
    }
    else
    {
        $cc = sprintf("%." . $dic . "f", $n);
    }
    return $cc;
}

function getnumberamount($uid, $lotto_id, $bettype, $num, $order = 0)
{
    switch ($bettype)
    {
        case 0 :
        case 24 :
            $rs = getdb("!!select sum(amount) from bet_amounts_6h where numbers='" . d2($num) . "' and bettype in (0,24) and user_id=" . $uid . " and lotto_id=" . $lotto_id);
            break;
        case 25 :
        case 26 :
            $rs = getdb("!!select sum(amount) from bet_amounts_6h where numbers='" . $order . " " . d2($num) . "' and bettype in (25,26) and user_id=" . $uid . " and lotto_id=" . $lotto_id);
            break;
        case 4 :
        case 31 :
            $rs = getdb("!!select sum(amount) from bet_amounts_6h where numbers='" . d2($num) . "' and bettype in (4,31) and user_id=" . $uid . " and lotto_id=" . $lotto_id);
            break;
        default :
            $rs = getdb("!!select sum(amount) from bet_amounts_6h where numbers='" . d2($num) . "' and bettype=" . $bettype . " and user_id=" . $uid . " and lotto_id=" . $lotto_id);
            break;
    }
    $n = $rs === false ? 99999999 : $rs->f[0];
    $rs->close();
    return $n;
}

function get_protmode($userid, $gametype = 0)
{
    $info = get_user($userid, $gametype);
    $acl = $info['acl'];
    $protmod = 0;
    switch ($acl)
    {
        case 2 :
            $genagent = get_user($info['prev_id'], $gametype);
            $shareowner = get_user($genagent['prev_id'], $gametype);
            $subsidary = get_user($shareowner['prev_id'], $gametype);
            $protmod = $subsidary['protmod'];
            break;
        case 3 :
            $shareowner = get_user($info['prev_id'], $gametype);
            $subsidary = get_user($shareowner['prev_id'], $gametype);
            $protmod = $subsidary['protmod'];
            break;
        case 4 :
            $subsidary = get_user($info['prev_id'], $gametype);
            $protmod = $subsidary['protmod'];
            break;
        case 5 :
            $protmod = $info['protmod'];
            break;
    }
    return $protmod;
}

function get_rate0_mode($userid)
{
    global $conf;
    $info = get_user($userid);
    $acl = $info['acl'];
    $op = 0;
    if (intval($conf->get("reat0_open")) == 1)
    {
        switch ($acl)
        {
            case 1 :
                $agent = get_user($info['prev_id']);
                $genagent = get_user($agent['prev_id']);
                $shareowner = get_user($genagent['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                $op = $subsidary['reat0_open'];
                break;
            case 2 :
                $genagent = get_user($info['prev_id']);
                $shareowner = get_user($genagent['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                $op = $subsidary['reat0_open'];
                break;
            case 3 :
                $shareowner = get_user($info['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                $op = $subsidary['reat0_open'];
                break;
            case 4 :
                $subsidary = get_user($info['prev_id']);
                $op = $subsidary['reat0_open'];
                break;
            case 5 :
                $op = $info['reat0_open'];
                break;
        }
    }
    return $op;
}

function get_share_rate0($userid)
{
    global $conf;
    $info = get_user($userid);
    $acl = $info['acl'];
    $op = 0;
    if (intval($conf->get("reat0_open")) == 1)
    {
        switch ($acl)
        {
            case 1 :
                $agent = get_user($info['prev_id']);
                $genagent = get_user($agent['prev_id']);
                $shareowner = get_user($genagent['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                $op = $info['rate0'] + $agent['rate0'] + $genagent['rate0'] + $shareowner['rate0'] + $subsidary['rate0'];
                if ($subsidary['reat0_open'] == 0)
                {
                    $op = $subsidary['rate0'];
                }
                break;
            case 2 :
                $genagent = get_user($info['prev_id']);
                $shareowner = get_user($genagent['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                $op = $info['rate0'] + $genagent['rate0'] + $shareowner['rate0'] + $subsidary['rate0'];
                if ($subsidary['reat0_open'] == 0)
                {
                    $op = $subsidary['rate0'];
                }
                break;
            case 3 :
                $shareowner = get_user($info['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                $op = $info['rate0'] + $shareowner['rate0'] + $subsidary['rate0'];
                if ($subsidary['reat0_open'] == 0)
                {
                    $op = $subsidary['rate0'];
                }
                break;
            case 4 :
                $subsidary = get_user($info['prev_id']);
                $op = $info['rate0'] + $subsidary['rate0'];
                if ($subsidary['reat0_open'] == 0)
                {
                    $op = $subsidary['rate0'];
                }
                break;
            case 5 :
                $op = $info['rate0'];
                break;
        }
    }
    return $op;
}

function get_share_rate0_8($userid)
{
    global $conf;
    $info = get_user($userid);
    $acl = $info['acl'];
    $n = 0.04;
    $op = 0;
    if (intval($conf->get("reat0_open")) == 1)
    {
        switch ($acl)
        {
            case 1 :
                $agent = get_user($info['prev_id']);
                $genagent = get_user($agent['prev_id']);
                $shareowner = get_user($genagent['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                if ($subsidary['reat0_8_open'] == 1)
                {
                    $op = $n;
                }
                break;
            case 2 :
                $genagent = get_user($info['prev_id']);
                $shareowner = get_user($genagent['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                if ($subsidary['reat0_8_open'] == 1)
                {
                    $op = $n;
                }
                break;
            case 3 :
                $shareowner = get_user($info['prev_id']);
                $subsidary = get_user($shareowner['prev_id']);
                if ($subsidary['reat0_8_open'] == 1)
                {
                    $op = $n;
                }
                break;
            case 4 :
                $subsidary = get_user($info['prev_id']);
                if ($subsidary['reat0_8_open'] == 1)
                {
                    $op = $n;
                }
                break;
            case 5 :
                if ($info['reat0_8_open'] == 1)
                {
                    $op = $n;
                }
                break;
        }
    }
    return $op;
}

function get_lotto($id)
{
    static $cache = NULL;
    if (!isset($cache))
    {
        $cache = array();
    }
    if (!isset($cache[$id]))
    {
        $cache[$id] = new lotto($id);
    }
    return $cache[$id];
}

function copy_rate($table, $prev_id, $id)
{
    echo "<li>{$table}, {$prev_id}, {$id}";
    $rs = getdb("select * from {$table} where lotto_id={$prev_id}");
    if ($rs->eof)
    {
        getdb("insert into {$table} (lotto_id) values (0)");
        $rs = getdb("!!select * from {$table} where lotto_id=0");
    }
    while (!$rs->eof)
    {
        $f = array();
        $f['lotto_id'] = $id;
        foreach ($rs->f as $key => $val)
        {
            if (!is_int($key) && $key != "lotto_id")
            {
                $f[$key] = $val;
            }
        }
        db_insert($table, $f);
        $rs->next();
    }
}

function get_subsidary_id($userid)
{
    $info = get_user($userid);
    $acl = $info['acl'];
    $op = 0;
    switch ($acl)
    {
        case 1 :
            $agent = get_user($info['prev_id']);
            $genagent = get_user($agent['prev_id']);
            $shareowner = get_user($genagent['prev_id']);
            $subsidary = get_user($shareowner['prev_id']);
            $op = $subsidary['id'];
            break;
        case 2 :
            $genagent = get_user($info['prev_id']);
            $shareowner = get_user($genagent['prev_id']);
            $subsidary = get_user($shareowner['prev_id']);
            $op = $subsidary['id'];
            break;
        case 3 :
            $shareowner = get_user($info['prev_id']);
            $subsidary = get_user($shareowner['prev_id']);
            $op = $subsidary['id'];
            break;
        case 4 :
            $subsidary = get_user($info['prev_id']);
            $op = $subsidary['id'];
            break;
        case 5 :
            $op = $userid;
    }
    return $op;
}

function get_srates_data($userid, $lotto_id, $bettype, $order = 1)
{
    $subsid = get_subsidary_id($userid);
    $rs = false;
    switch ($bettype)
    {
        case 0 :
        case 24 :
            $rs = getdb("select * from rate0s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate0s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
                $rs = getdb("select * from rate0s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            }
            break;
        case 1 :
        case 2 :
        case 3 :
        case 13 :
        case 14 :
        case 18 :
        case 19 :
        case 20 :
        case 21 :
        case 22 :
        case 37 :
        case 38 :
        case 28 :
        case 30 :
        case 34 :
        case 35 :
        case 46 :
        case 47 :
            $rs = getdb("select * from rate1s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate1s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
                $rs = getdb("select * from rate1s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            }
            break;
        case 4 :
        case 31 :
            $rs = getdb("select * from rate4s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate4s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
                $rs = getdb("select * from rate4s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            }
            break;
        case 5 :
        case 6 :
            $rs = getdb("select * from rate5s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate5s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
                $rs = getdb("select * from rate5s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            }
            break;
        case 25 :
        case 26 :
            $rs = getdb("select * from rate4" . $order . "s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate4" . $order . "s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
                $rs = getdb("select * from rate4" . $order . "s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            }
            break;
        case 15 :
        case 16 :
        case 17 :
        case 23 :
            $rs = getdb("select * from rate15s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate15s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
                $rs = getdb("select * from rate15s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            }
            break;
    }
    return $rs;
}

function get_subs_rates($userid, $lotto_id, $bettype, $number, $order = 1)
{
    $op = 0;
    $subsid = get_subsidary_id($userid);
    switch ($bettype)
    {
        case 0 :
        case 24 :
            $rs = getdb("select * from rate0s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate0s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
            }
            else
            {
                $op = $rs->f["r" . d2($number)];
            }
            break;
        case 1 :
        case 2 :
        case 3 :
        case 13 :
        case 14 :
        case 18 :
        case 19 :
        case 20 :
        case 21 :
        case 37 :
        case 22 :
        case 38 :
        case 28 :
        case 30 :
        case 34 :
        case 35 :
        case 46 :
        case 47 :
            $rs = getdb("select * from rate1s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate1s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
            }
            else
            {
                if ($bettype == 37)
                {
                    $bettype = 21;
                }
                if ($bettype == 38)
                {
                    $bettype = 22;
                }
                if ($bettype == 19 && strpos($number, "|"))
                {
                    $p1 = explode("|", $number);
                    $op = $rs->f["r" . $bettype . "_" . $p1[0]];
                }
                else
                {
                    $op = $rs->f["r" . $bettype . "_" . $number];
                }
            }
            break;
        case 4 :
        case 31 :
            $rs = getdb("select * from rate4s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate4s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
            }
            else
            {
                $op = $rs->f["r" . d2($number)];
            }
            break;
        case 5 :
        case 6 :
            $rs = getdb("select * from rate5s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate5s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
            }
            else
            {
                $op = $rs->f["r" . $bettype . "_" . $number];
            }
            break;
        case 25 :
        case 26 :
            $rs = getdb("select * from rate4" . $order . "s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate4" . $order . "s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
            }
            else
            {
                $op = $rs->f["r" . d2($number)];
            }
            break;
        case 15 :
        case 16 :
        case 17 :
        case 23 :
            $rs = getdb("select r15_" . $number . " from rate15s where lotto_id=" . $lotto_id . " and user_id=" . $subsid);
            if ($rs->eof)
            {
                getdb("insert into rate15s (lotto_id, user_id) values (" . $lotto_id . ", " . $subsid . ")");
            }
            else
            {
                $op = $rs->f[0];
            }
            break;
    }
    return floatval($op);
}

function get_all_upercent($user_id, $acl, $percent0, $percent1, $mpercent, $gametype = 0)
{
    static $users = NULL;
    $err = 0;
    if (!isset($users))
    {
        $users = getuserlist($user_id, 2);
    }
    if (2 < $acl && $users != "")
    {
        $rss = getdb("select * from k_user K left join k_user_percent P ON (P.user_id = K.id and P.gametype={$gametype}) where K.enabled=1 and K.acl=2 and K.id in (" . $users . ")");
        while (!$rss->eof && $err == 0)
        {
            $u2 = get_user($rss->f['id'], $gametype);
            $u3 = get_user($u2['prev_id'], $gametype);
            $u4 = get_user($u3['prev_id'], $gametype);
            $u5 = get_user($u4['prev_id'], $gametype);
            switch ($acl)
            {
                case 3 :
                    $r_genagent = $percent0 + $mpercent;
                    if ($r_genagent < $u2['percent0'] + $u2['mpercent'])
                    {
                        ++$err;
                    }
                    if ($u4['mpercent'] < $r_genagent)
                    {
                        ++$err;
                    }
                    $r_shareowner = min($percent1, $u4['mpercent'] - $r_genagent);
                    $r_subsidary = min($u4['percent1'], $u5['mpercent'] - $r_shareowner - $r_genagent);
                    break;
                case 4 :
                    $r_genagent = $u2['percent0'] + $u2['mpercent'];
                    if (0 < $mpercent + $percent0 + $percent1 && ( $u5['mpercent'] < $mpercent + $percent0 || $percent1 < $percent0 ))
                    {
                        ++$err;
                    }
                    $r_shareowner = min($u3['percent1'], $mpercent - $r_genagent);
                    $r_subsidary = min($u4['percent1'], $u5['mpercent'] - $r_shareowner - $r_genagent);
                    if ($mpercent < $r_genagent)
                    {
                        ++$err;
                    }
                    break;
                case 5 :
                    $r_genagent = $u2['percent0'] + $u2['mpercent'];
                    $r_shareowner = min($u3['percent1'], $u4['mpercent'] - $r_genagent);
                    $r_subsidary = min($u4['percent1'], $u5['mpercent'] - $r_shareowner - $r_genagent);
                    if ($mpercent < $u4['percent0'] + $u4['mpercent'])
                    {
                        ++$err;
                    }
                    if ($mpercent < $u3['mpercent'] + $u3['percent0'] + $u4['percent0'])
                    {
                        ++$err;
                    }
                    if ($mpercent < $r_genagent + $u3["percent0" . $f3d_mod] + $u4["percent0" . $f3d_mod])
                    {
                        ++$err;
                    }
                    break;
            }
            if ($u5['mpercent'] < $r_subsidary + $r_shareowner + $r_genagent)
            {
                ++$err;
            }
            $err_list = " uid={$user_id}, acl={$acl}, percent0={$percent0}, percent1={$percent1}, mpercent={$mpercent} <br>";
            if ($err)
            {
                $err_list .= "请检查下列帐号设置异常: gametype={$gametype} <br>";
                if (2 < $acl)
                {
                    $err_list .= "acl=代理 ,id=" . $u2['id'] . " ,帐号=" . $u2['account'] . " ,最小占成=" . $u2['percent0'] . " ,最大占成=" . $u2['mpercent'] . "<br>";
                }
                if (3 < $acl)
                {
                    $err_list .= "acl=总代 ,id=" . $u3['id'] . " ,帐号=" . $u3['account'] . " ,最小占成=" . $u3['percent0'] . " ,最大占成=" . $u3['mpercent'] . "<br>";
                }
                if (4 < $acl)
                {
                    $err_list .= "acl=股东 ,id=" . $u4['id'] . " ,帐号=" . $u4['account'] . " ,最小占成=" . $u4['percent0'] . " ,最大占成=" . $u4['mpercent'] . "<br>";
                }
            }
            $rss->movenext();
        }
    }
    if ($err)
    {
        diemsg($err_list);
    }
    return $err;
}

function get_listurl($open, $bettype, $numbers)
{
    $url = "op.php?op=agent&fp=betlist&dt1=" . $open . "&dt2=" . $open . "&bettype=" . $bettype . "&numbers=" . urlencode($numbers);
    return $url;
}

function get_listurl2($open, $bettype, $numbers, $ord = 1)
{
    $url = "op.php?op=agent&fp=betlist1&dt1=" . $open . "&dt2=" . $open . "&bettype=" . $bettype . "&page=-1&order=" . $ord . "&numbers=" . $numbers;
    return $url;
}

function get_listurl4($open, $bettype, $norder, $numbers)
{
    $url = "op.php?op=agent&fp=betlist&dt1=" . $open . "&dt2=" . $open . "&bettype=" . $bettype . "&order=" . $norder . "&numbers=" . urlencode($numbers);
    return $url;
}

function get_listurl5($open, $bettype, $numbers)
{
    if ($bettype == 9)
    {
        $bettype = 55;
    }
    else if ($bettype == 10)
    {
        $bettype = 66;
    }
    $url = "op.php?op=agent&fp=betlist1&dt1=" . $open . "&dt2=" . $open . "&bettype=" . $bettype . "&page=-1&order=2&numbers=" . $numbers;
    return $url;
}

function get_listurl6($open, $type, $num)
{
    $numbers = "{$type}|{$num}";
    $url = "op.php?op=agent&fp=betlist1&dt1=" . $open . "&dt2=" . $open . "&bettype=27&page=-1&order=2&numbers=" . $numbers;
    return $url;
}

function get_LM_Array($bettype, $num)
{
    $len = count($num);
    $ntrs = array();
    if ($bettype == 29)
    {
        $i = 0;
        while ($i < $len - 4)
        {
            $j = $i + 1;
            while ($j < $len - 3)
            {
                $k = $j + 1;
                while ($k < $len - 2)
                {
                    $l = $k + 1;
                    while ($l < $len - 1)
                    {
                        $m = $l + 1;
                        while ($m < $len)
                        {
                            $ntrs[] = $num[$i] . "," . $num[$j] . "," . $num[$k] . "," . $num[$l] . "," . $num[$m];
                            ++$m;
                        }
                        ++$l;
                    }
                    ++$k;
                }
                ++$j;
            }
            ++$i;
        }
    }
    else if ($bettype == 8 || $bettype == 9)
    {
        $i = 0;
        while ($i <= $len - 2)
        {
            $j = $i + 1;
            while ($j <= $len - 2)
            {
                $k = $j + 1;
                while ($k < $len)
                {
                    $ntrs[] = $num[$i] . "," . $num[$j] . "," . $num[$k];
                    ++$k;
                }
                ++$j;
            }
            ++$i;
        }
    }
    else
    {
        $i = 0;
        while ($i <= $len - 2)
        {
            $j = $i + 1;
            while ($j < $len)
            {
                $ntrs[] = $num[$i] . "," . $num[$j];
                ++$j;
            }
            ++$i;
        }
    }
    return $ntrs;
}

function Scan_LM_Array($bettype, $num, $ary, $maxnum)
{
    $n = 0 - 1;
    $j = 0;
    while ($j < $maxnum)
    {
        if ($num == $ary[$bettype][$j]['num'])
        {
            $n = $j;
            $j = $maxnum;
        }
        ++$j;
    }
    return $n;
}

function Empty_LM_Array($bettype, $ary, $maxnum)
{
    $n = 0 - 1;
    $j = 0;
    while ($j < $maxnum)
    {
        if ($ary[$bettype][$j]['num'] == "")
        {
            $n = $j;
            $j = $maxnum;
        }
        ++$j;
    }
    return $n;
}

function show_live($conf)
{
    $rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
    if ($rs->eof)
    {
        return false;
    }
    if (0 < $rs->f['total'])
    {
        return false;
    }
    $open = $rs->f['open'];
    $round = $rs->f['round'];
    $lotto_id = $rs->f['id'];
    $stopbet = $conf->get("waitbet");
    $rs->close();
    if (date("Y-m-d") < $open)
    {
        return false;
    }
    if (date("Y-m-d") == $open && $stopbet <= date("Hi"))
    {
        return $round;
    }
    return false;
}

function get_bet0_money($userid, $n)
{
    $uid = $userid;
    if (0 < $uid && $uid < 5)
    {
        $uid = get_subsidary_id($userid);
    }
    $op = 0;
    $nn = d2(intval($n));
    $rsn = getdb("select r" . $nn . " from maxbet0 WHERE user_id=" . $uid);
    if (!$rsn->eof)
    {
        $op = intval($rsn->f[0]);
    }
    $rsn->close();
    return $op;
}

function err($str, $c = "")
{
    $color = $c == "" ? "#ff0000" : $c;
    diemsg("<table border=0 cellpadding=6 cellspacing=0>" . "<tr><td bgcolor=" . $color . "><font color=white><b>" . $str . "</b></font></td></tr></table><br>");
    exit();
}

function account_err($str, $c = "")
{
    $color = $c == "" ? "#ff0000" : $c;
    diemsg("<table border=0 cellpadding=6 cellspacing=0>" . "<tr><td bgcolor=" . $color . "><font color=white><b>" . $str . "</b></font></td></tr></table><br>", "", "?op=member&fp=account");
    exit();
}

function n_to_c($number)
{
    if (10000 <= $number)
    {
        if (100000000 <= $number)
        {
            $aa = substr($number, 0 - 4);
            if (intval($aa) == 0)
            {
                $aa = "元";
            }
            $b1 = substr($number, 0, 0 - 8);
            $b2 = substr($number, 0 - 8, 0 - 4);
            if (intval($aa + $b2) == 0)
            {
                $number = $b1 . "亿元";
            }
            else
            {
                $number = $b1 . "亿" . $b2 . "万" . $aa;
            }
        }
        else
        {
            $aa = substr($number, 0 - 4);
            if (intval($aa) == 0)
            {
                $aa = "元";
            }
            $bb = substr($number, 0, 0 - 4);
            $number = $bb . "万" . $aa;
        }
    }
    return $number;
}

function show_numbercolor($n, $dic = 0)
{
    $cc = "";
    if ($n < 0)
    {
        $cc = "<font color=Fuchsia>" . sprintf("%." . $dic . "f", $n) . "</font>";
    }
    else
    {
        $cc = "<font color=Green>" . sprintf("%." . $dic . "f", $n) . "</font>";
    }
    return $cc;
}

function bet2in1($uid, $acl, $pid, $btype, $num, $money, $rsfile, $ratefd, $u2, $u3, $u4, $u5, $stime, $lt_id = 0)
{
    global $lotto_id;
    global $prot_start;
    global $autoport_mode;
    global $conf;
    global $g_rate0_8num;
    if ($lt_id != 0)
    {
        $lotto_id = $lt_id;
    }
    $fields = array();
    $fields['user_id'] = $uid;
    $fields['prev_id'] = $pid;
    $fields['acl'] = $acl;
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $btype;
    $fields['numbers'] = "'" . $num . "'";
    $fields['amount'] = $money;
    $fields['line_nums'] = "'" . $num . "'";
    $fields['unitmoney'] = $money;
    $bttt = $btype;
    switch ($btype)
    {
        case 24 :
            $bttt = 0;
            break;
        case 26 :
            $bttt = 25;
            break;
        case 31 :
            $bttt = 4;
            break;
        case 37 :
            $bttt = 21;
            break;
        case 38 :
            $bttt = 22;
            break;
    }
    $subrates = 0;
    $rsu = get_srates_data($uid, $lotto_id, $btype);
    if ($rsu->eof)
    {
        $subrates = 0;
    }
    else
    {
        $subrates = $rsu->f["r" . $ratefd];
    }
    $fields['rate2'] = 0;
    $fields['rate3'] = 0;
    $fields['rate4'] = 0;
    $fields['rate5'] = 0;
    $rate0_8num = 0;
    $percent_sw = $u5['percent_sw'];
    if ($u5['reat0_8_open'] == 1)
    {
        $rate0_8num = $g_rate0_8num;
    }
    switch ($acl)
    {
        case 2 :
            $fields['uperid3'] = $u2['prev_id'];
            $fields['uperid4'] = $u3['prev_id'];
            $fields['uperid5'] = $u4['prev_id'];
            if ($btype == 0 || $btype == 24)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate2'] = $u2['rate0'];
                    $fields['rate3'] = $u3['rate0'];
                    $fields['rate4'] = $u4['rate0'];
                }
                $subrates += $u5['rate0'];
            }
            else if (0 < $btype && $btype < 4 || $btype == 20 || $btype == 28 || $btype == 30)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate2'] = $u2['rate0'] * $rate0_8num;
                    $fields['rate3'] = $u3['rate0'] * $rate0_8num;
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
                $subrates += $u5['rate0'] * $rate0_8num;
            }
            $fields['rate5'] = $subrates;
            $fields['back2'] = getback($u2['id'], $bttt, 1, 2);
            $fields['back3'] = getback($u3['id'], $bttt, 1, 3);
            $fields['back4'] = getback($u4['id'], $bttt, 1, 4);
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = $u2['percent2'];
            $fields['percent4'] = round(( 100 - $fields['percent3'] ) * $u3['percent2'] / 100, 0);
            $fields['percent5'] = round(( 100 - $fields['percent3'] - $fields['percent4'] ) * $u4['percent2'] / 100, 0);
            $fields['r2'] = $money * $fields['percent3'] / 100;
            $fields['r3'] = $money * $fields['percent4'] / 100;
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 3 :
            $fields['uperid3'] = $uid;
            $fields['uperid4'] = $u3['prev_id'];
            $fields['uperid5'] = $u4['prev_id'];
            if ($btype == 0 || $btype == 24)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate3'] = $u3['rate0'];
                    $fields['rate4'] = $u4['rate0'];
                }
                $subrates += $u5['rate0'];
            }
            else if (0 < $btype && $btype < 4 || $btype == 20 || $btype == 28 || $btype == 30)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate3'] = $u3['rate0'] * $rate0_8num;
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
                $subrates += $u5['rate0'] * $rate0_8num;
            }
            $fields['rate5'] = $subrates;
            $fields['back2'] = 100;
            $fields['back3'] = getback($u3['id'], $bttt, 1, 3);
            $fields['back4'] = getback($u4['id'], $bttt, 1, 4);
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = $u3['percent2'];
            $fields['percent5'] = round(( 100 - $fields['percent4'] ) * $u4['percent2'] / 100, 0);
            $fields['r3'] = $money * $fields['percent4'] / 100;
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 4 :
            $fields['uperid4'] = $uid;
            $fields['uperid5'] = $u4['prev_id'];
            if ($btype == 0 || $btype == 24)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate4'] = $u4['rate0'];
                }
                $subrates += $u5['rate0'];
            }
            else if (0 < $btype && $btype < 4 || $btype == 20 || $btype == 28 || $btype == 30)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
                $subrates += $u5['rate0'] * $rate0_8num;
            }
            $fields['rate5'] = $subrates;
            $fields['back2'] = 100;
            $fields['back3'] = 100;
            $fields['back4'] = getback($u4['id'], $bttt, 1, 4);
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = 0;
            $fields['percent5'] = $u4['percent2'];
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 5 :
            $fields['uperid5'] = $uid;
            if ($btype == 0 || $btype == 24)
            {
                $fields['rate5'] = $u5['rate0'];
            }
            else if (0 < $btype && $btype < 4 || $btype == 20 || $btype == 28 || $btype == 30)
            {
                $fields['rate5'] = $u5['rate0'] * $rate0_8num;
            }
            $fields['back2'] = 100;
            $fields['back3'] = 100;
            $fields['back4'] = 100;
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = 0;
            $fields['percent5'] = 0;
            break;
    }
    $fields['percent255'] = max(100 - $fields['percent5'] - $fields['percent4'] - $fields['percent3'], 0);
    $fields['r6'] = $money * $fields['percent255'] / 100;
    $fields['betmode'] = 0;
    $rsx = getdb("!!select * from rate" . $rsfile . "a where lotto_id=" . $lotto_id);
    $fields['rate'] = $rsx->f["r" . $ratefd] - $fields['rate2'] - $fields['rate3'] - $fields['rate4'] - $fields['rate5'] - 0;
    $rsx->close();
    if ($fields['rate'] <= 0 || $fields['amount'] <= 0)
    {
        return 0 - 1;
    }
    $newid = db_insert("bet2", $fields);
    $fields['id'] = $newid;
    check_autorate($lotto_id, $btype, $num, $fields['r6']);
    return $fields;
}

function bet2in2($uid, $acl, $pid, $btype, $order, $num, $money, $rsfile, $ratefd, $u2, $u3, $u4, $u5, $stime, $lt_id = 0)
{
    global $lotto_id;
    global $prot_start;
    global $autoport_mode;
    global $conf;
    global $g_rate0_8num;
    if ($lt_id != 0)
    {
        $lotto_id = $lt_id;
    }
    $fields = array();
    $fields['user_id'] = $uid;
    $fields['prev_id'] = $pid;
    $fields['acl'] = $acl;
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $btype;
    if ($btype == 19 || $btype == 27 || $btype == 36)
    {
        $fields['numbers'] = "'{$order}|{$num}'";
    }
    else
    {
        $fields['numbers'] = "'{$order} {$num}'";
    }
    $fields['amount'] = $money;
    $fields['line_nums'] = "'{$num}'";
    $fields['unitmoney'] = $money;
    $bttt = $btype;
    if ($btype == 24)
    {
        $bttt = 0;
    }
    if ($btype == 26)
    {
        $bttt = 25;
    }
    if ($btype == 27)
    {
        $bttt = intval("27" . $order);
    }
    if ($btype == 36)
    {
        $bttt = intval("36" . substr($order, 0, 1));
    }
    $subrates = 0;
    $nonRateArr = array(25, 362, 363, 364, 272, 273, 274, 275);
    if (!in_array($bttt, $nonRateArr))
    {
        $rsu = get_srates_data($uid, $lotto_id, $btype, $order);
        $subrates = $rsu === false ? 0 : $rsu->f["r" . $ratefd];
    }
    $fields['rate2'] = 0;
    $fields['rate3'] = 0;
    $fields['rate4'] = 0;
    $fields['rate5'] = 0;
    $percent_sw = $u5['percent_sw'];
    $rate0_8num = 0;
    if ($u5['reat0_8_open'] == 1)
    {
        $rate0_8num = $g_rate0_8num;
    }
    switch ($acl)
    {
        case 2 :
            $fields['uperid3'] = $u2['prev_id'];
            $fields['uperid4'] = $u3['prev_id'];
            $fields['uperid5'] = $u4['prev_id'];
            if ($btype == 19)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate2'] = $u2['rate0'] * $rate0_8num;
                    $fields['rate3'] = $u3['rate0'] * $rate0_8num;
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
                $subrates += $u5['rate0'] * $rate0_8num;
                $fields['rate5'] = $subrates;
            }
            else if ($btype == 25 || $btype == 26)
            {
                $fields['rate5'] = $subrates;
            }
            $fields['back2'] = getback($u2['id'], $bttt, 1, 2);
            $fields['back3'] = getback($u3['id'], $bttt, 1, 3);
            $fields['back4'] = getback($u4['id'], $bttt, 1, 4);
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = $u2['percent2'];
            $fields['percent4'] = round(( 100 - $fields['percent3'] ) * $u3['percent2'] / 100, 0);
            $fields['percent5'] = round(( 100 - $fields['percent3'] - $fields['percent4'] ) * $u4['percent2'] / 100, 0);
            $fields['r2'] = $money * $fields['percent3'] / 100;
            $fields['r3'] = $money * $fields['percent4'] / 100;
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 3 :
            $fields['uperid3'] = $uid;
            $fields['uperid4'] = $u3['prev_id'];
            $fields['uperid5'] = $u4['prev_id'];
            if ($btype == 19)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate3'] = $u3['rate0'] * $rate0_8num;
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
                $subrates += $u5['rate0'] * $rate0_8num;
                $fields['rate5'] = $subrates;
            }
            else if ($btype == 25 || $btype == 26)
            {
                $fields['rate5'] = $subrates;
            }
            $fields['back2'] = 100;
            $fields['back3'] = getback($u3['id'], $bttt, 1, 3);
            $fields['back4'] = getback($u4['id'], $bttt, 1, 4);
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = $u3['percent2'];
            $fields['percent5'] = round(( 100 - $fields['percent4'] ) * $u4['percent2'] / 100, 0);
            $fields['r3'] = $money * $fields['percent4'] / 100;
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 4 :
            $fields['uperid4'] = $uid;
            $fields['uperid5'] = $u4['prev_id'];
            if ($btype == 19)
            {
                if (intval($conf->get("reat0_open")) == 1 && $u5['reat0_open'] == 1)
                {
                    $fields['rate4'] = $u4['rate0'] * $rate0_8num;
                }
                $subrates += $u5['rate0'] * $rate0_8num;
                $fields['rate5'] = $subrates;
            }
            else if ($btype == 25 || $btype == 26)
            {
                $fields['rate5'] = $subrates;
            }
            $fields['back2'] = 100;
            $fields['back3'] = 100;
            $fields['back4'] = getback($u4['id'], $bttt, 1, 4);
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = 0;
            $fields['percent5'] = $u4['percent2'];
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 5 :
            $fields['uperid5'] = $uid;
            if ($btype == 19)
            {
                $fields['rate5'] = $u5['rate0'] * $rate0_8num;
            }
            $fields['back2'] = 100;
            $fields['back3'] = 100;
            $fields['back4'] = 100;
            $fields['back5'] = getback($u5['id'], $bttt, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bttt, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = 0;
            $fields['percent5'] = 0;
            break;
    }
    $fields['percent255'] = max(100 - $fields['percent5'] - $fields['percent4'] - $fields['percent3'], 0);
    $fields['r6'] = $money * $fields['percent255'] / 100;
    $fields['betmode'] = 0;
    if ($btype == 27 || $btype == 36)
    {
        $fields['rate'] = $ratefd - 0;
    }
    else
    {
        $rsx = getdb("!!select * from rate" . $rsfile . "a where lotto_id=" . $lotto_id);
        $fields['rate'] = $rsx->f["r" . $ratefd] - $fields['rate2'] - $fields['rate3'] - $fields['rate4'] - $fields['rate5'] - 0;
        $rsx->close();
    }
    if ($fields['rate'] <= 0 || $fields['amount'] <= 0)
    {
        return 0 - 1;
    }
    $newid = db_insert("bet2", $fields);
    if ($btype == 19)
    {
        check_autorate($lotto_id, $btype, "{$order}|{$num}", $fields['r6']);
    }
    else if ($btype != 27 && $btype != 36)
    {
        check_autorate($lotto_id, $btype, "{$order} {$num}", $fields['r6']);
    }
    $fields['id'] = $newid;
    return $fields;
}

function bet2in3($uid, $acl, $pid, $bettype, $betcounts, $num, $money, $rate, $u2, $u3, $u4, $u5, $stime)
{
    global $lotto_id;
    global $prot_start;
    global $autoport_mode;
    global $g_rate0_8num;
    $fields = array();
    $fields['user_id'] = $uid;
    $fields['prev_id'] = $pid;
    $fields['acl'] = $acl;
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'" . $money . "|" . $rate . "|" . $betcounts . "|" . $num . "'";
    $fields['amount'] = $money * $betcounts;
    $fields['line_nums'] = "'{$num}'";
    $fields['unitmoney'] = $money;
    if (strpos($rate, ",") == 0)
    {
        $fields['rate'] = $rate;
    }
    else
    {
        $p = explode(",", $rate);
        $fields['rate'] = $p[0] - 0;
    }
    if ($fields['rate'] <= 0 || $fields['amount'] <= 0)
    {
        return 0 - 1;
    }
    $percent_sw = $u5['percent_sw'];
    switch ($acl)
    {
        case 2 :
            $fields['uperid3'] = $u2['prev_id'];
            $fields['uperid4'] = $u3['prev_id'];
            $fields['uperid5'] = $u4['prev_id'];
            $fields['back2'] = getback($u2['id'], $bettype, 1, 2);
            $fields['back3'] = getback($u3['id'], $bettype, 1, 3);
            $fields['back4'] = getback($u4['id'], $bettype, 1, 4);
            $fields['back5'] = getback($u5['id'], $bettype, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bettype, 1, 255);
            $fields['percent3'] = $u2['percent2'];
            $fields['percent4'] = round(( 100 - $fields['percent3'] ) * $u3['percent2'] / 100, 0);
            $fields['percent5'] = round(( 100 - $fields['percent3'] - $fields['percent4'] ) * $u4['percent2'] / 100, 0);
            $fields['r2'] = $money * $fields['percent3'] / 100;
            $fields['r3'] = $money * $fields['percent4'] / 100;
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 3 :
            $fields['uperid3'] = $uid;
            $fields['uperid4'] = $u3['prev_id'];
            $fields['uperid5'] = $u4['prev_id'];
            $fields['back2'] = 100;
            $fields['back3'] = getback($u3['id'], $bettype, 1, 3);
            $fields['back4'] = getback($u4['id'], $bettype, 1, 4);
            $fields['back5'] = getback($u5['id'], $bettype, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bettype, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = $u3['percent2'];
            $fields['percent5'] = round(( 100 - $fields['percent4'] ) * $u4['percent2'] / 100, 0);
            $fields['r3'] = $money * $fields['percent4'] / 100;
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 4 :
            $fields['uperid4'] = $uid;
            $fields['uperid5'] = $u4['prev_id'];
            $fields['back2'] = 100;
            $fields['back3'] = 100;
            $fields['back4'] = getback($u4['id'], $bettype, 1, 4);
            $fields['back5'] = getback($u5['id'], $bettype, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bettype, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = 0;
            $fields['percent5'] = $u4['percent2'];
            $fields['r4'] = $money * $fields['percent5'] / 100;
            break;
        case 5 :
            $fields['uperid5'] = $uid;
            $fields['back2'] = 100;
            $fields['back3'] = 100;
            $fields['back4'] = 100;
            $fields['back5'] = getback($u5['id'], $bettype, 1, 5);
            $fields['back6'] = getback($u5['supe_account'], $bettype, 1, 255);
            $fields['percent3'] = 0;
            $fields['percent4'] = 0;
            $fields['percent5'] = 0;
            break;
    }
    $fields['percent255'] = max(100 - $fields['percent5'] - $fields['percent4'] - $fields['percent3'], 0);
    $fields['r6'] = $money * $fields['percent255'] / 100;
    $fields['betmode'] = 0;
    db_insert("bet2", $fields);
    $tmpArr = array(29, 39, 40, 41, 42, 43, 44, 45);
    if (in_array($bettype, $tmpArr))
    {
        foreach (explode(",", $num) as $numVal)
        {
            check_autorate($lotto_id, $bettype, $numVal, $fields['r6']);
        }
    }
}

function auto_prots($acl, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $type = 0)
{
    global $lotto_id;
    global $g_rate0_8num;
    $conf = new Config();
    $betmin = $conf->get("betmin");
    $prot_start = $conf->get("protect_start");
    $autoport_mode = $conf->get("autoport_mod");
    if ($prot_start == 0 || $autoport_mode == 0)
    {
        return 0;
    }
    $op = 0;
    switch ($acl)
    {
        case 2 :
            if ($u2['mpercent'] == 0)
            {
                return $op;
            }
            $uid = $u2['id'];
            $pid = $u2['prev_id'];
            break;
        case 3 :
            if ($u2['percent2'] == 0)
            {
                return $op;
            }
            $uid = $u3['id'];
            $pid = $u3['prev_id'];
            break;
        case 4 :
            if ($u3['percent2'] == 0)
            {
                return $op;
            }
            $uid = $u4['id'];
            $pid = $u4['prev_id'];
            break;
        case 5 :
            if ($u4['percent2'] == 0 || $u5['percent_sw'] == 1)
            {
                return $op;
            }
            $uid = $u5['id'];
            $pid = $u5['prev_id'];
            break;
        default :
            return $op;
    }
    $mod = 1;
    $autont = 0;
    $betdoc = "";
    $rsfile = "";
    $ratefd = $num;
    $myamount = GetAmountSetting($uid, 0, 0);
    switch ($btype)
    {
        case 24 :
            $autont = $myamount[0]['auto_nt'];
            break;
        case 26 :
            $autont = $myamount[25]['auto_nt'];
            break;
        case 31 :
            $autont = $myamount[4]['auto_nt'];
            break;
        case 32 :
            $autont = $myamount[10]['auto_nt'];
            break;
        case 33 :
            $autont = $myamount[9]['auto_nt'];
            break;
        case 27 :
            $autont = $myamount[intval($btype . $type)]['auto_nt'];
            break;
        case 36 :
            $autont = $myamount[intval($btype . substr(strval($type), 0, 1))]['auto_nt'];
            break;
        case 37 :
            $autont = $myamount[21]['auto_nt'];
            break;
        case 38 :
            $autont = $myamount[22]['auto_nt'];
            break;
        default :
            $autont = $myamount[$btype]['auto_nt'];
            break;
    }
    if ($autont <= 0)
    {
        return $op;
    }
    switch ($btype)
    {
        case 0 :
        case 24 :
            $betdoc = "bettype in (0,24) and numbers='" . $num . "'";
            $rsfile = 0;
            break;
        case 1 :
        case 2 :
        case 3 :
        case 13 :
        case 14 :
        case 18 :
        case 20 :
        case 28 :
        case 30 :
        case 34 :
        case 35 :
            $betdoc = "bettype=" . $btype . " and numbers='" . $num . "'";
            $rsfile = 1;
            $ratefd = $btype . "_" . $num;
            break;
        case 21 :
        case 37 :
            $betdoc = "bettype in (21,37) and numbers='" . $num . "'";
            $rsfile = 1;
            $ratefd = $btype . "_" . $num;
            break;
        case 22 :
        case 38 :
            $betdoc = "bettype in (22,38) and numbers='" . $num . "'";
            $rsfile = 1;
            $ratefd = $btype . "_" . $num;
            break;
        case 4 :
        case 31 :
            $betdoc = "bettype in (4,31) and numbers='" . $num . "'";
            $rsfile = 4;
            break;
        case 5 :
        case 6 :
            $betdoc = "bettype=" . $btype . " and numbers='" . $num . "'";
            $rsfile = 5;
            $ratefd = $btype . "_" . $num;
            break;
        case 7 :
        case 8 :
        case 11 :
        case 29 :
        case 39 :
        case 40 :
        case 41 :
        case 42 :
        case 43 :
        case 44 :
        case 45 :
            $betdoc = "bettype=" . $btype . " and line_nums='" . $num . "'";
            $mod = 3;
            break;
        case 9 :
        case 33 :
            $betdoc = "bettype in (9,33) and line_nums='" . $num . "'";
            $rsx = getdb("!!select * from rate5a where lotto_id=" . $lotto_id);
            if (!$rsx->eof)
            {
                $rate = $rsx->f['r9_1'] . "," . $rsx->f['r9_2'];
            }
            $rsx->close();
            $btype = 9;
            $mod = 3;
            break;
        case 10 :
        case 32 :
            $betdoc = "bettype in (10,32) and line_nums='" . $num . "'";
            $rsx = getdb("!!select * from rate5a where lotto_id=" . $lotto_id);
            if (!$rsx->eof)
            {
                $rate = $rsx->f['r10_1'] . "," . $rsx->f['r10_2'];
            }
            $rsx->close();
            $btype = 10;
            $mod = 3;
            break;
        case 19 :
            $betdoc = "bettype=" . $btype . " and numbers='" . $type . "|" . $num . "'";
            $rsfile = 1;
            $ratefd = $btype . "_" . $type;
            $mod = 2;
            break;
        case 15 :
        case 16 :
        case 17 :
        case 23 :
            $betdoc = "bettype=" . $btype . " and numbers='" . $type . " " . $num . "'";
            $rsfile = 15;
            $ratefd = "15_" . $type . "_" . $num;
            $mod = 2;
            break;
        case 25 :
        case 26 :
            $betdoc = "bettype in (25,26) and numbers='" . $type . " " . $num . "'";
            $rsfile = "4" . $type;
            $ratefd = $num;
            $mod = 2;
            break;
        case 27 :
            $betdoc = "bettype=" . $btype . " and numbers='" . $type . "|" . $num . "'";
            $rsfile = 27;
            $ratefd = $rate;
            $mod = 2;
            break;
        case 36 :
            $betdoc = "bettype=" . $btype . " and numbers='" . $type . "|" . $num . "'";
            $rsfile = 36;
            $ratefd = $rate;
            $mod = 2;
            break;
        default :
            return $op;
    }
    $users = getuserlist($uid);
    if ($users == "")
    {
        return $op;
    }
    switch ($acl)
    {
        case 5 :
            $rsx = getdb("!!select sum(amount*percent4/100) from bet where user_id in (" . $users . ") and lotto_id=" . $lotto_id . " and " . $betdoc . " and rame=0");
            break;
        case 4 :
            $rsx = getdb("!!select sum(amount*percent3/100) from bet where user_id in (" . $users . ") and lotto_id=" . $lotto_id . " and " . $betdoc . " and rame=0");
            break;
        case 3 :
            $rsx = getdb("!!select sum(amount*percent2/100) from bet where user_id in (" . $users . ") and lotto_id=" . $lotto_id . " and " . $betdoc . " and rame=0");
            break;
        case 2 :
            $rsx = getdb("!!select sum(amount*mpercent/100) from bet where user_id in (" . $users . ") and lotto_id=" . $lotto_id . " and " . $betdoc . " and rame=0");
            break;
    }
    $rr1 = $rsx->f[0] - 0;
    $rsx->close();
    $rr2 = 0;
    if (2 < $acl)
    {
        $rsx = getdb("!!select sum(amount*percent" . $acl . "/100) from bet2 where lotto_id=" . $lotto_id . " and uperid" . $acl . "=" . $uid . " and " . $betdoc . " and rame=0");
        $rr2 = $rsx->f[0] - 0;
        $rsx->close();
    }
    $rsx = getdb("!!select sum(amount) from bet2 where user_id=" . $uid . " and lotto_id=" . $lotto_id . " and " . $betdoc . " and rame=0");
    $rr3 = $rsx->f[0] - 0;
    $rsx->close();
    $usedamount = intval($rr1 + $rr2 - $rr3);
    if ($usedamount < $betmin || $usedamount <= 0)
    {
        return $op;
    }
    if ($autont <= $usedamount && 0 < $rate)
    {
        $op = intval($usedamount - $autont) - 0;
        if ($betmin <= $op)
        {
            switch ($mod)
            {
                case 1 :
                    bet2in1($uid, $acl, $pid, $btype, $num, $op, $rsfile, $ratefd, $u2, $u3, $u4, $u5, $stime);
                    break;
                case 2 :
                    bet2in2($uid, $acl, $pid, $btype, $type, $num, $op, $rsfile, $ratefd, $u2, $u3, $u4, $u5, $stime);
                    break;
                case 3 :
                    bet2in3($uid, $acl, $pid, $btype, $type, $num, $op, $rate, $u2, $u3, $u4, $u5, $stime);
                    break;
            }
        }
    }
    return $op;
}

function getline_number_rate($bettype, $num, $disk = "a")
{
    global $lotto_id;
    global $conf;
    $op = array(0, 0);
    $auto_line = sprintf("%.2f", floatval($conf->get("autorate_linema")));
    if ($auto_line <= 0 || 1 < $auto_line || $bettype == 8 || $bettype == 9 || $bettype == 29 || $bettype == 33)
    {
        return false;
    }
    $rs4a = getdb("select * from rate4" . $disk . " where lotto_id=0");
    $rs4b = getdb("select * from rate4" . $disk . " where lotto_id=" . $lotto_id);
    $rs5a = getdb("select * from rate5" . $disk . " where lotto_id=" . $lotto_id);
    $nn = d2(intval($num[0]));
    $rr = $rs4b->f["r" . $nn] - 0;
    if ($rs4b->f["r" . d2(intval($num[1]))] < $rr)
    {
        $nn = d2(intval($num[1]));
        $rr = $rs4b->f["r" . $nn] - 0;
    }
    if ($rs4a->f["r" . $nn] != $rr)
    {
        if ($bettype == 10)
        {
            $op[0] = sprintf("%.2f", zero($rs5a->f['r10_1'], $rs4a->f["r" . $nn]) * $auto_line * $rr);
            $ps = sprintf("%.2f", zero($rs5a->f['r10_2'], $rs4a->f["r" . $nn]) * $auto_line * $rr);
            $op[1] = $op[0] . "," . $ps;
        }
        else if ($bettype == 32)
        {
            $op[0] = sprintf("%.2f", zero($rs5a->f['r32_1'], $rs4a->f["r" . $nn]) * $auto_line * $rr);
            $ps = sprintf("%.2f", zero($rs5a->f['r32_2'], $rs4a->f["r" . $nn]) * $auto_line * $rr);
            $op[1] = $op[0] . "," . $ps;
        }
        else if ($bettype == 7 || $bettype == 11)
        {
            $op[0] = sprintf("%.2f", zero($rs5a->f["r" . $bettype . "_1"], $rs4a->f["r" . $nn]) * $auto_line * $rr);
            $op[1] = $op[0];
        }
        return $op;
    }
    return false;
}

function setRateRecord($fieldArr, $cid = -1)
{
    global $curruser;
    if (!isset($curruser['account']))
    {
        $acc = "-";
        $acl = 255;
        $id = 1;
        $ip = "";
    }
    else
    {
        $acc = $curruser['account'];
        $acl = $curruser['acl'];
        $id = $curruser['id'];
        $ip = getIP();
    }
    $sql = "INSERT INTO rate_record (user_account, user_id, uptime, method, gametype, round, lotto_id, betcontent, orate, nrate, ip, pagename, paperopen, acl)";
    $sql .= " VALUES (";
    $sql .= " '{$acc}'";
    $sql .= " ," . $id;
    $sql .= " ,'" . date("Y-m-d H:i:s") . "'";
    $sql .= " ,'" . $fieldArr['method'] . "'";
    $sql .= " ," . $fieldArr['gametype'];
    $sql .= " ,'" . $fieldArr['round'] . "'";
    $sql .= " ," . $fieldArr['lotto_id'];
    $sql .= " ,'" . $fieldArr['betcontent'] . "'";
    $sql .= " ," . $fieldArr['orate'];
    $sql .= " ," . $fieldArr['nrate'];
    $sql .= " ,'{$ip}'";
    $sql .= " ,'" . $fieldArr['pagename'] . "'";
    $sql .= " ,'" . $fieldArr['paperopen'] . "'";
    $sql .= " ,'{$acl}'";
    $sql .= ")";
    if ($cid < 0)
    {
        getdb($sql);
    }
    else
    {
        getdb_cen($cid, $sql);
    }
}

function GetBetContentForSetRateRecord($rateField)
{
    global $g_betname;
    global $g_bignum;
    global $g_chinayear;
    $p1 = explode("_", $rateField);
    $bettype = intval(substr($p1[0], 1));
    $reVal = "";
    switch ($bettype)
    {
        case 19 :
            $tempNum = $p1[1] . "_" . $p1[2] . "|" . str_pad("", $p1[2] * 2 - 1, "0,");
            $tempStr = getbetcontent($bettype, $tempNum);
            if ($endI = strpos($tempStr, "<"))
            {
                $reVal = substr($tempStr, 0, $endI);
            }
            else
            {
                $reVal = $tempStr;
            }
            break;
        case 15 :
        case 16 :
        case 17 :
        case 23 :
            $tempNum = $p1[1] . " " . $p1[2];
            $reVal = getbetcontent($bettype, $tempNum);
            break;
        case 8 :
            $reVal = $g_betname[$bettype];
            break;
        case 9 :
        case 33 :
            $c2c3Arr = array("1" => "中二", "2" => "中三");
            $reVal = $g_betname[$bettype] . $c2c3Arr[$p1[1]];
            break;
        case 7 :
        case 11 :
        case 29 :
        case 39 :
        case 40 :
        case 41 :
        case 42 :
        case 43 :
        case 44 :
        case 45 :
            $reVal = $g_betname[$bettype];
            break;
        case 10 :
        case 32 :
            $c2c3Arr = array("1" => "中特", "2" => "中二");
            $reVal = $g_betname[$bettype] . $c2c3Arr[$p1[1]];
            break;
        case 27 :
            $reVal = $p1[1] . "肖:" . $g_chinayear[intval($p1[2])];
            break;
        case 36 :
            $subidArr = array(21 => "二尾", 31 => "三尾", 41 => "四尾", 22 => "二尾不中", 32 => "三尾不中", 42 => "四尾不中","51"=>"五尾");
            //$reVal = $subidArr[$p1[1]] . ":" . intval($p1[2]) . "尾";
			$reVal=$subidArr[$p1[1]];
            break;
        default :
            $reVal = getbetcontent($bettype, $p1[1]);
            break;
    }
    return $reVal;
}

function getIP()
{
    $ip = FALSE;
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip)
        {
            array_unshift($ips, $ip);
            $ip = FALSE;
        }
        $i = 0;
        while ($i < count($ips))
        {
            if (!preg_match("/^(?:10|172\\.(?:1[6-9]|2\\d|3[01])|192\\.168)\\./", $ips[$i]))
            {
                if (version_compare(phpversion(), "5.0.0", ">="))
                {
                    if (ip2long($ips[$i]) != false)
                    {
                        $ip = $ips[$i];
                        break;
                    }
                }
                if (ip2long($ips[$i]) != 0 - 1)
                {
                    $ip = $ips[$i];
                    break;
                }
            }
            ++$i;
        }
    }
    return $ip ? $ip : $_SERVER['REMOTE_ADDR'];
}

function check_upid_mode($userid)
{
    $info = get_user($userid);
    $acl = $info['acl'];
    $op = 1;
    switch ($acl)
    {
        case 1 :
            $agent = get_user($info['prev_id']);
            $genagent = get_user($agent['prev_id']);
            $shareowner = get_user($genagent['prev_id']);
            $subsidary = get_user($shareowner['prev_id']);
            if ($agent['enabled'] == 0 || $genagent['enabled'] == 0 || $shareowner['enabled'] == 0 || $subsidary['enabled'] == 0)
            {
                $op = 0;
            }
            break;
        case 2 :
            $genagent = get_user($info['prev_id']);
            $shareowner = get_user($genagent['prev_id']);
            $subsidary = get_user($shareowner['prev_id']);
            if ($genagent['enabled'] == 0 || $shareowner['enabled'] == 0 || $subsidary['enabled'] == 0)
            {
                $op = 0;
            }
            break;
        case 3 :
            $shareowner = get_user($info['prev_id']);
            $subsidary = get_user($shareowner['prev_id']);
            if ($shareowner['enabled'] == 0 || $subsidary['enabled'] == 0)
            {
                $op = 0;
            }
            break;
        case 4 :
            $subsidary = get_user($info['prev_id']);
            if ($subsidary['enabled'] == 0)
            {
                $op = 0;
            }
            break;
    }
    return $op;
}

function check_account_legal($account, $len = 6)
{
    return 1;
    while ($i < $lens)
    {
        $cc = substr($account, $i, 1);
        $c_ok += substr_count("abcdefghijklmnopqrstuvwxyz", $cc);
        $n_ok += substr_count("1234567890", $cc);
        ++$i;
    }
    if ($c_ok == 0 || $n_ok == 0 || $n_ok + $c_ok != $lens)
    {
        $op = 0;
    }
    return $op;
}

function chkBet_peace($chkArr)
{
    $chk = 0;
    if (isset($chkArr['a1']) && $chkArr['a1'] == 0)
    {
        ++$chk;
    }
    else
    {
        return false;
    }
    if (isset($chkArr['b1']) && $chkArr['b1'] == 0)
    {
        ++$chk;
    }
    else
    {
        return false;
    }
    if (isset($chkArr['rame']) && $chkArr['rame'] == 9)
    {
        ++$chk;
    }
    else
    {
        return false;
    }
    if ($chk)
    {
        return true;
    }
    return false;
}

function getUserValidDiskSelect($curruser, $fun_name = "")
{
    if (!is_array($curruser))
    {
        echo "-1,ID ERROR! (CODE: -1)";
        exit();
    }
    $disk = intval(getvar("disk"));
    if ($disk == "")
    {
        $diskArr = array(
            0,
            $curruser['disk1'],
            $curruser['disk2'],
            $curruser['disk3'],
            $curruser['disk4']
        );
        foreach ($diskArr as $key => $value)
        {
            if ($value == 0)
            {
                continue;
            }
            $disk = $key;
            break;
        }
    }
    $select_disk = "<div class=\"col-sm-2\"><select class=\"form-control\" style=\"height: 25px; padding: 0 0;\" onChange=\"{$fun_name}\" id=\"select_disk\" name=\"disk\">";
    $select_disk .= $curruser['disk1'] == 1 ? "<option value=\"1\"" . ( $disk == 1 ? "selected" : "" ) . ">A盘</option>" : "";
    $select_disk .= $curruser['disk2'] == 1 ? "<option value=\"2\"" . ( $disk == 2 ? "selected" : "" ) . ">B盘</option>" : "";
    $select_disk .= $curruser['disk3'] == 1 ? "<option value=\"3\"" . ( $disk == 3 ? "selected" : "" ) . ">C盘</option>" : "";
    $select_disk .= $curruser['disk4'] == 1 ? "<option value=\"4\"" . ( $disk == 4 ? "selected" : "" ) . ">D盘</option>" : "";
    $select_disk .= "</select></div>";
    return $select_disk;
}

function getUserValidDisk($curruser)
{
    if (!is_array($curruser))
    {
        echo "-1,ID ERROR! (CODE: -1)";
        setcookie("disk", $disk, systime() - 3600);
        exit();
    }
    $disk = intval(getvar("disk"));
    if ($disk == "")
    {
        $diskArr = array(0, $curruser['disk1'], $curruser['disk2'], $curruser['disk3'], $curruser['disk4']);
        foreach ($diskArr as $key => $value)
        {
            if ($value == 0)
            {
                continue;
            }
            return $key;
            break;
        }
    }
    if ($disk < 1 || 4 < $disk)
    {
        echo "-3,DISK ERROR! (CODE: -3)";
        setcookie("disk", $disk, systime() - 3600);
        exit();
    }
    if ($curruser["disk" . $disk] != 1)
    {
        echo "-4,DISK ERROR! (CODE: -4)";
        setcookie("disk", $disk, systime() - 3600);
        exit();
    }
    setcookie("disk", $disk, systime() + 3600);
    return $disk;
}

function QuotedStr($str)
{
    return "'" . trim($str, "'") . "'";
}

function debug_log($str)
{
    $add_str = $str . "\n";
    if (!( $fp = fopen("debug_log.txt", "a") ))
    {
        echo "open error";
    }
    else
    {
        fwrite($fp, $add_str);
        fpassthru($fp);
    }
}

function pt($arr) {
    echo '<pre/>';
    print_r($arr);
    exit;
}

?>